//########################################################################
//## (C) by Edwin Vriethoff under Microsoft Reciprocal License (Ms-RL)  ##
//## See http://www.codeplex.com/SPUserPoll/license for full license    ##
//##                                                                    ##
//## http://edwin.vriethoff.net http://www.codeplex.com/SPUserPoll/     ##
//########################################################################

using System;
using System.Security.Cryptography;
using System.Text;
using Microsoft.SharePoint;
using SPUserPoll.Data.Components;

namespace SPUserPoll.Business.Components
{
    /// <summary>
    /// User functions
    /// </summary>
    public class User
    {
        const string c_AnonymousSalt = "SPUserPollUser";

        /// <summary>
        /// Gets the login for the user
        /// </summary>
        /// <returns></returns>
        public static string GetUserLogin()
        {
            return SPContext.Current.Web.CurrentUser.LoginName;
        }

        /// <summary>
        /// Check if the user has rights to modify the poll status
        /// </summary>
        /// <returns></returns>
        public static bool HasDesignerRights()
        {
            SPRoleDefinitionBindingCollection usersRoles = SPContext.Current.Web.AllRolesForCurrentUser;
            SPRoleDefinitionCollection roleDefinitions = SPContext.Current.Web.RoleDefinitions;

            //Find the roles for Designer and Administrator
            SPRoleDefinition roleDefinitionDesign = null;
            SPRoleDefinition roleDefinitionAdmin = null;
            
            foreach (SPRoleDefinition roleDef in roleDefinitions)
            {
                if (roleDef.Type == SPRoleType.Administrator)
                    roleDefinitionAdmin = roleDef;

                if (roleDef.Type == SPRoleType.WebDesigner)
                    roleDefinitionDesign = roleDef;

                if (roleDefinitionAdmin != null && roleDefinitionDesign != null)
                    break;
            }

            //If roles have not been found we can only check if the user is siteadmin
            if (roleDefinitionAdmin == null && roleDefinitionDesign == null)
            {
                if (SPContext.Current.Web.CurrentUser.IsSiteAdmin)
                    return true;
                else 
                    return false;

            }
            else if(roleDefinitionAdmin == null && roleDefinitionDesign != null) 
            {
                //roleadmin has not been found we can only check roledefdesign and siteadmin
                if (usersRoles.Contains(roleDefinitionDesign) || SPContext.Current.Web.CurrentUser.IsSiteAdmin)
                    return true;
                else
                    return false;
            }
            else if (roleDefinitionAdmin != null && roleDefinitionDesign == null)
            {
                //roledesign has not been found we can only check on admin and siteadmin
                if (usersRoles.Contains(roleDefinitionAdmin) || SPContext.Current.Web.CurrentUser.IsSiteAdmin)
                    return true;
                else
                    return false;
            }
            else
            {
                //we can run the full check
                if (usersRoles.Contains(roleDefinitionDesign) || usersRoles.Contains(roleDefinitionAdmin) || SPContext.Current.Web.CurrentUser.IsSiteAdmin)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// Check if the user has administrative rights on the site
        /// </summary>
        /// <returns></returns>
        public static bool HasAdminRights()
        {
            SPRoleDefinitionBindingCollection usersRoles = SPContext.Current.Web.AllRolesForCurrentUser;
            SPRoleDefinitionCollection roleDefinitions = SPContext.Current.Web.RoleDefinitions;


            //Find the role for Administrator
            SPRoleDefinition roleDefinitionAdmin = null;

            foreach (SPRoleDefinition roleDef in roleDefinitions)
            {
                if (roleDef.Type == SPRoleType.Administrator)
                {
                    roleDefinitionAdmin = roleDef;
                    break;
                }
           }

           //if no admin role on the site has been found we can only check if the user is siteadmin
           if (roleDefinitionAdmin == null)
           {
               if (SPContext.Current.Web.CurrentUser.IsSiteAdmin)
                   return true;
               else
                   return false;
           }
           else
           {
               //check if the user is an admin or a siteadmin
               if (usersRoles.Contains(roleDefinitionAdmin) || SPContext.Current.Web.CurrentUser.IsSiteAdmin)
               {
                   return true;
               }
               else
               {
                   return false;
               }
           }
        }

        /// <summary>
        /// Checks if the user did already vote
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pollID"></param>
        /// <returns></returns>
        public static bool DidTheUserVote(string userName, string pollID, bool anonymous)
        {
            if (anonymous == true)
                userName = CreateAnonymousName(userName);

            ResponseList responseList = new ResponseList();
            return responseList.DidTheUserVote(userName, pollID);
        }

        /// <summary>
        /// Creates a anonymous username
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static string CreateAnonymousName(string userName)
        {
            userName = userName + c_AnonymousSalt;
            MD5CryptoServiceProvider crypt = new MD5CryptoServiceProvider();
            return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(crypt.ComputeHash(ASCIIEncoding.Default.GetBytes(userName))), "-", "");
        }
    }
}
